package com.ruoyi.flow.service.impl;

import com.gitee.deeround.jdbchelper.DbAction;
import com.ruoyi.core.entity.UserContext;
import com.ruoyi.core.util.JsonUtil;
import com.ruoyi.flow.domain.Machine;
import com.ruoyi.flow.entity.Constant;
import com.ruoyi.flow.entity.machine.Event;
import com.ruoyi.flow.entity.machine.Flow;
import com.ruoyi.flow.entity.machine.Line;
import com.ruoyi.flow.entity.machine.Node;
import com.ruoyi.flow.exception.FlowException;
import com.ruoyi.flow.service.FlowMachineService;
import com.ruoyi.flow.service.FlowUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class FlowMachineServiceImpl implements FlowMachineService {

    @Autowired
    private FlowUserService flowUserService;

    @Autowired
    private DbAction dbAction;

    @Override
    public List<Machine> getMachines() {
        String sql = "select * from flow_machine";

        return dbAction.queryForListObject(sql, Machine.class);
    }

    @Override
    public Machine getMachine(String machineId) {
        String sql = "select * from flow_machine where machine_id=?";

        return dbAction.queryForObject(sql, Machine.class, machineId);
    }

    @Override
    public void copyMachine(Machine machine) {
        Flow flow = JsonUtil.toBean(machine.getMachineJson(), Flow.class);

        String oldMachineId = flow.getId();
        String newMachineId = machine.getMachineId();

        flow.setId(machine.getMachineId());
        flow.setName(machine.getMachineName());
        String json = JsonUtil.toJson(flow);
        publish(json);

        dbAction.copyTable("flow_" + oldMachineId, "flow_" + newMachineId);
    }

    @Override
    public Flow getFlow(String machineId) {
        Machine machine = getMachine(machineId);

        if (machine == null) {
            throw new FlowException("未找到流程模板：" + machineId);
        }

        return JsonUtil.toBean(machine.getMachineJson(), Flow.class);
    }

    @Override
    public void publish(String machineJson) {
        Flow flow = JsonUtil.toBean(machineJson, Flow.class);

        //对流程模板进行标准化处理
        for (Line line : flow.getLines()) {
            if (line.getSeq() == null) {
                line.setSeq(99);
            }
        }
        for (Node node : flow.getNodes()) {
            if (node.getBack() == null) {
                node.setBack(Constant.Bool.NO);
            }
        }
        for (Event event : flow.getEvents()) {
            if (event.getSeq() == null) {
                event.setSeq(99);
            }
            if (event.getSelected() == null) {
                event.setSelected(Constant.Bool.NO);
            }
        }

        String sql = "delete from flow_machine where machine_id=?";
        dbAction.update(sql, new Object[]{flow.getId()});

        sql = "insert into flow_machine(machine_id,machine_name,machine_json,create_dept,create_by,create_time,update_by,update_time) values(?,?,?,?,?,?,?,?)";

        UserContext userContext = flowUserService.currentUser();
        Date now = new Date();
        dbAction.update(sql, new Object[]{

                flow.getId(),
                flow.getName(),
                JsonUtil.toJson(flow),
                userContext.getDeptId(),
                userContext.getUserId(),
                now,
                userContext.getUserId(),
                now

        });
    }
}
